import 'package:get/get.dart';
import 'package:meiyunapp/entity/goods_detail_entity.dart';
import 'package:meiyunapp/net/api_service.dart';
import 'package:meiyunapp/net/base/base_entity.dart';
import 'package:meiyunapp/net/base/error_entity.dart';
import 'package:meiyunapp/net/api/goods_http.dart';
import 'package:meiyunapp/service/store_services.dart';

class GoodDetailController extends SuperController<GoodsDetailEntity> {
  var id;

  var tHttp = Get.find<GoodsHttp>();

  @override
  void onInit() {
    print("onInit-->$id");
    var name = StoreService.to.getValue<String>("name");

    print("onInit-->$name");
    super.onInit();
  }

  @override
  void onReady() {
    print("onReady-->$id");
    super.onReady();
  }

  void getNewData(String id) async {
    this.id = id;
    BaseEntity<GoodsDetailEntity> data = await tHttp.getData(id);
    if (data.state == 0) {
      change(data.data, status: RxStatus.success());
    } else if (data.state == 999) {
      change(null, status: RxStatus.error("${data.errorEntity?.message}"));
    } else {
      change(null, status: RxStatus.error(data.msg));
    }
  }

  void getData(String id) {
    this.id = id;
    ApiService.instance.request<GoodsDetailEntity>(
      "pms/product/detail",
      params: {"id": id},
      success: (GoodsDetailEntity data) {
        change(data, status: RxStatus.success());
      },
      error: (ErrorEntity entity) {
        change(null, status: RxStatus.error(entity.message));
      },
    );
  }

  @override
  void onDetached() {
    print("onDetached-->$id");
  }

  @override
  void onInactive() {
    print("onInactive-->$id");
  }

  @override
  void onPaused() {
    print("onPaused-->$id");
  }

  @override
  void onResumed() {
    print("onResumed-->$id");
  }
}
